//  this library.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hktk_app/dao/my_dao.dart';
import 'package:hktk_app/dao/package_dao.dart';
import 'package:hktk_app/exception/dao_exception.dart';
import 'package:hktk_app/model/package/column_package_model.dart';
import 'package:hktk_app/model/package/package_model.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/toast_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/package/purchase_widget.dart';

class ContentTitleWidget extends StatelessWidget {
  /// 标题名称
  final String title;

  /// 该科目是否购买，true：购买/免费，false：未购买
  final bool free;

  /// 栏目id
  final String columnId;

  const ContentTitleWidget({
    super.key,
    required this.title,
    required this.free,
    required this.columnId,
  });

  @override
  Widget build(BuildContext context) {

    return Container(
      // height: px100,
      padding: EdgeInsets.symmetric(horizontal: 40, vertical: 20),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(20),
          bottomRight: Radius.circular(20),
        ),
        boxShadow: [
          BoxShadow(
            color: colorBlack4,
            offset: Offset(0, 2),
            blurRadius: 8, // 阴影模糊程度
            spreadRadius: 0, // 阴影扩散程度
          ),
        ],
      ),
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          // Expanded(
          //   child: Text(
          //     title,
          //     style: FontUtil.fontBlack1(34, fontWeight: FontWeight.w500),
          //     maxLines: 2,
          //     overflow: TextOverflow.ellipsis,
          //   ),
          // ),
          // hiSpace(width: 10),
          free ? _downloadItem(context) : _buyBtnItem(context),
        ],
      ),
    );
  }

  /// 购买按钮组件
  _buyBtnItem(BuildContext context) {
    return GestureDetector(
      onTap: () {
        _buyBtnFun(context);
      },
      child: Container(
        width: 150,
        height: 50,
        decoration: BoxDecoration(
          color: colorBlue1,
          borderRadius: BorderRadius.circular(30),
        ),
        child: Center(
          child: Text(
            "去购买",
            style: FontUtil.fontWhile1(
              30,
              fontWeight: FontWeight.w400,
            ),
          ),
        ),
      ),
    );
  }

  /// 标题下载组件
  _downloadItem(BuildContext context) {
    return Container();
    // return GestureDetector(
    //   onTap: () {
    //     _downBtnFun(context);
    //   },
    //   child: Icon(
    //     Icons.download,
    //     size: px40,
    //   ),
    // );
  }

  /// 去购买 按钮点击方法
  void _buyBtnFun(BuildContext context) {
    // 开启加载框
    ToastUtil.loadingBoxShow(context);
    // 加载数据
    _getPackage().then((Map<String, dynamic> map) {
      // 关闭加载框
      ToastUtil.loadingBoxRemove();
      if (map["suc"]) {
        // 加载成功，弹出购买弹窗
        _onTipsSetState(
          context,
          packageModel: map["package"],
          payHide: map["payHide"],
        );
      } else {
        // 加载失败
        ToastUtil.show(context: context, msg: map["msg"]);
      }
    });
  }

  /// 下载按钮 点击方法
  void _downBtnFun(BuildContext context) {
  }

  /// 请求方法
  Future<Map<String, dynamic>> _getPackage() async {
    Map<String, dynamic> map = {};
    try {
      // 获取套餐相关信息
      ColumnPackageModel model = await PackageDao.getPackage(
        columnId: columnId,
        examId: dataUtilComm.dataModel.examId,
        categoryId: dataUtilComm.dataModel.categoryId,
      );
      if (model.package != null) {
        model.package!.skuModels?.forEach((SkuModel sku) {
          bool sss = listEquals(model.productIds, sku.productIds);
         sku.isSelect = sss;
        });

        // 获取是否需要隐藏支付方式
        bool payHide = await MyDao.versionCurrentIos();
        map["package"] = model.package;
        map["payHide"] = payHide;
        map["suc"] = true;
      } else {
        map["suc"] = false;
        map["msg"] = "未知错误，请重试";
      }
    } catch (e) {
      map = {"suc": false, "msg": "未知错误，请重试"};
      if (e is DaoException) {
        map["msg"] = e.msg;
      }
    }
    return map;
  }

  /// 弹出购买框方法
  void _onTipsSetState(
      BuildContext context, {
        required PackageModel packageModel,
        required bool payHide,
      }) {
    // 获取弹出层高度
    var height = ScreenUtil().screenHeight;
    double sheetHeight = height * 0.8;
    showModalBottomSheet(
      context: context,
      // 设置底部弹出框全屏显示
      isScrollControlled: true,
      // 自定义弹出层高度
      constraints: BoxConstraints(maxHeight: sheetHeight),
      // 自定义圆角
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(30),
          topRight: Radius.circular(30),
        ),
      ),
      builder: (BuildContext context) {
        // return PurchaseWidget(
        //   package: packageModel,
        //   index: 0,
        //   payHide: payHide,
        // );
        return Text("1111111");
      },
    );
  }
}
